package com.qyhl.webtv.module_live.teletext;


import android.util.Log;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.live.TeleTextBean;
import com.qyhl.webtv.module_live.common.LiveUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/12/14.
 */

public class TeleTextListModel implements TeleTextListContract.TeleTextListModel {
    private TeleTextListPresenter mPresenter;

    TeleTextListModel(TeleTextListPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取图文直播列表
     */
    @Override
    public void getDatas(final String sceneId, String type) {
        EasyHttp.get(LiveUrl.GET_TELETEXT_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("sceneId", sceneId)
                .params("type", type)
                .execute(new SimpleCallBack<List<TeleTextBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！", !sceneId.equals("0"));
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！", !sceneId.equals("0"));
                        }
                    }

                    @Override
                    public void onSuccess(List<TeleTextBean> teleTextBeans) {
                        if (teleTextBeans != null && teleTextBeans.size() > 0) {
                            mPresenter.setDatas(teleTextBeans, !sceneId.equals("0"));
                        } else {
                            mPresenter.setError(2, "暂无任何内容！", !sceneId.equals("0"));
                        }
                    }
                });

    }

    @Override
    public void checkCode(String sceneId, String code, TeleTextBean bean) {
        EasyHttp.post(LiveUrl.GET_TELETEXT_CHECK)
                .params("sceneId", sceneId)
                .params("psw", code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.checkResult(false, bean);
                    }

                    @Override
                    public void onSuccess(String s) {
                        mPresenter.checkResult(true, bean);
                    }
                });
    }
}
